#include "stable.h"
#include "QkComicView.h"

QkComicView::QkComicView()
{
	init();
}

QkComicView::QkComicView(QWidget *parent) : QkBaseView(parent)
{
	init();
}

QkComicView::~QkComicView()
{
}

QKindleViewId QkComicView::viewId() const
{
	return QKindleComicView;
}

void QkComicView::init()
{
	m_pixmapItem = NULL;

	QAction *prevAction = new QAction("Prev", this);
	prevAction->setObjectName("prevAction");
	m_actions.append(prevAction);
	QAction *nextAction = new QAction("Next", this);
	nextAction->setObjectName("nextAction");
	m_actions.append(nextAction);

	QString path;
	path = qApp->applicationDirPath();
	path += "/data/[Comic][CityHunter][Vol_01]/";

	m_folder = path;
	QDir dir(path);

	QString filter("*.png");
	m_imageList = dir.entryList();
	m_index = m_imageList.count();
	m_index = 0;

	QVBoxLayout *layout = new QVBoxLayout();

	QGraphicsScene *scene = new QGraphicsScene();
	m_graphicView = new QGraphicsView(scene, this);
	scene->setSceneRect(0, 0, 240, 320);

	layout->addWidget(m_graphicView);
	layout->setMargin(0);
	setLayout(layout);

	QMetaObject::connectSlotsByName(this);
}

void QkComicView::mouseReleaseEvent(QMouseEvent *event)
{
	Q_UNUSED(event);

	if(event->button() == Qt::LeftButton)
	{
		m_index = (m_index + 1) % m_imageList.count();

		QPixmap image(m_folder + m_imageList[m_index]);
		m_graphicView->scene()->addPixmap(image);
	}
	else 
	{
		emit navigate(this, "toc");
	}
}

void QkComicView::activated()
{
	toolBar()->setActions(m_actions);
}

void QkComicView::deactivated()
{
}

void QkComicView::on_prevAction_triggered()
{
	if(m_index == 0)
	{
		return;
	}
	m_index = (m_index - 1) % m_imageList.count();
	updateView();
}

void QkComicView::on_nextAction_triggered()
{
	m_index = (m_index + 1) % m_imageList.count();
	updateView();
}

void QkComicView::updateView()
{
	QPixmap p(m_folder + m_imageList[m_index]);
	QRect rc = p.rect();
	rc.setWidth(rc.width() / 2);
	QPixmap image = p.copy(rc);

	if(m_pixmapItem == NULL)
	{
		m_pixmapItem = m_graphicView->scene()->addPixmap(image);
	}
	else
	{
		m_pixmapItem->setPixmap(image);
	}

	updatePixmapLayout();
}

void QkComicView::resizeEvent(QResizeEvent *event)
{
	QRect rc(0, 0, event->size().width() - 2, event->size().height() - 2);
	m_graphicView->scene()->setSceneRect(rc);

	updatePixmapLayout();
}

qreal centralAlignScale(const QRectF &rect, const QSize &size)
{
	return qMin(rect.width() / size.width(), rect.height() / size.height());
}

QPointF centralAlignPos(const QRectF &rect, const QSize &size)
{
	qreal scale = centralAlignScale(rect, size);
	qreal x = (rect.width() - size.width() * scale) / 2;
	qreal y = (rect.height() - size.height() * scale) / 2;

	return QPointF(x, y);
}

void QkComicView::updatePixmapLayout()
{
	if(m_pixmapItem == NULL)
	{
		return;
	}

	QRectF sceneRect = m_graphicView->scene()->sceneRect();
	QSize pixmapSize = m_pixmapItem->pixmap().size();

	qreal scale = centralAlignScale(sceneRect, pixmapSize);
	QPointF pos = centralAlignPos(sceneRect, pixmapSize);

	m_pixmapItem->setTransform(QTransform::fromScale(scale, scale));
	m_pixmapItem->setPos(pos);
}